﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public class SoldierAttackState:ISoldierState
{

    public SoldierAttackState(SoldierFSMSytem fsm,ICharacter c) : base(fsm,c) { 
        mStateID = SoldierStateID.Attack;
        mAttackTimer = mAttackTime;
    }
    private float mAttackTime = 1;//攻击间隔
    private float mAttackTimer = 1;// 攻击计时器

    // 满足条件(事件),进行转换
    public override void Reason(List<ICharacter> targets)
    {
        // 敌人消失/敌人死亡,场上已经没有敌人了
        if (targets == null || targets.Count == 0)
        {
            mFSM.PerformTransition(SoldierTransition.NoEnmey); return;
        }
        float distance = Vector3.Distance(mCharacter.position, targets[0].position);
        // 消灭敌人/敌人消失后仍有敌人,且距离在攻击范围外,进行状态切换
        if (distance > mCharacter.atkRange)
        {
            mFSM.PerformTransition(SoldierTransition.SeeEnemy); 
        }
    }
    // 执行状态,攻击敌人
    public override void Act(List<ICharacter> targets)
    {
        if (targets == null || targets.Count == 0) return;
        mAttackTimer += Time.deltaTime;
        if (mAttackTimer >= mAttackTime)
        {
            mCharacter.Attack(targets[0]);
            mAttackTimer = 0;
        }
    }
}
